﻿using DataAccess.News;
using FOModels.News;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Bussiness.News
{
    public class NewsBLL
    {
        private NewsDAL dal;
        public NewsBLL()
        {
            dal = new NewsDAL();
        }
        public List<NewsModel> getReferenceNews(int top, int productid, int newsid, int langid)
        {
            List<NewsModel> list = new List<NewsModel>();
            NewsModel fur;
            DataTable tb = dal.getReferenceNews(top, productid,newsid, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                    fur.CompanyName = row["Name"].ToString();
                    list.Add(fur);
                }

            }
            return list;

        }
        public List<NewsModel> getTop3NewsByNotID(int langid, int newsid)
        {
            List<NewsModel> list = new List<NewsModel>();
            NewsModel fur;
            DataTable tb = dal.getTop3NewsByNotID(langid, newsid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                    fur.ProductCateName = row["Name"].ToString();
                    fur.ProductName = row["Name"].ToString();
                    list.Add(fur);
                }

            }
            return list;

        }
        public NewsModel getNewsByIDToGet(int NewsID, int langid)
        {
            NewsModel fur = new NewsModel();
            DataTable tb = dal.getNewsByIDToGet(NewsID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                    fur.CompanyName = row["Name"].ToString();
                    fur.ProductCID = int.Parse(row["ProductCID"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public NewsModel getTop1NewsByID(int langid)
        {
            NewsModel fur = new NewsModel();
            DataTable tb = dal.getTop1NewsByID(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                    fur.CompanyName = row["Name"].ToString();
                    fur.ProductCID = int.Parse(row["ProductCID"].ToString());
                    fur.ProductCateName = row["Name"].ToString();
                    fur.ProductName = row["Name"].ToString();
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public NewsModel getNewsByID(int NewsID, int langid)
        {
            NewsModel fur = new NewsModel();
            DataTable tb = dal.getNewsByID(NewsID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    if (row["UpdateDate"].ToString() != "")
                    {
                        fur.UpdateDate = DateTime.Parse(row["UpdateDate"].ToString());
                    }
                    else
                    {
                        fur.UpdateDate = null; 
                    }
                    fur.UpdateUser = row["UpdateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public NewsModel getTOPNewsByNewsHot(int top,int langid)
        {
            NewsModel fur = new NewsModel();
            DataTable tb = dal.getTOPNewsByNewsHot(top,langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public NewsModel getTOPNewsByNewsHotAndProCate(int top, int ProductCID, int langid)
        {
            NewsModel fur = new NewsModel();
            DataTable tb = dal.getTOPNewsByNewsHotAndProCate(top, ProductCID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                    fur.ProductCID = int.Parse(row["ProductCID"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                    fur.ProductCateName = row["Name"].ToString();
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public List<NewsModel> getNews(int langid)
        {
            List<NewsModel> list = new List<NewsModel>();
            NewsModel fur;
            DataTable tb = dal.getNews(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                    fur.CompanyName = row["Name"].ToString();
                    list.Add(fur);
                }
               
            }
            return list;

        }
        public List<NewsModel> getNewsByProCate(int ProductCID,int langid)
        {
            List<NewsModel> list = new List<NewsModel>();
            NewsModel fur;
            DataTable tb = dal.getNewsByProCate(ProductCID,langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.ProductID = int.Parse(row["ProductID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                    fur.CreateUser = row["CreateUser"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.NewsDeadline = row["NewsDeadline"].ToString();
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    fur.NewsSumary = WebUtility.HtmlDecode(row["NewsSumary"].ToString());
                    fur.NewsContent = WebUtility.HtmlDecode(row["NewsContent"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                    fur.CompanyName = row["Name"].ToString();
                    fur.ProductCID = int.Parse(row["ProductCID"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                    fur.ProductCateName = row["Name"].ToString();
                    list.Add(fur);
                }

            }
            return list;

        }
        public List<NewsModel> getCoutNewsByCate(int productid)
        {
            List<NewsModel> list = new List<NewsModel>();
            NewsModel fur;
            DataTable tb = dal.getNewsByCate(productid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    list.Add(fur);
                }

            }
            return list;

        }
        public NewsModel getTOP1()
        {
            NewsModel fur = new NewsModel();
            DataTable tb = dal.gettop1news();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public List<NewsModel> getall(int langid)
        {
            List<NewsModel> list = new List<NewsModel>();
            NewsModel fur;
            DataTable tb = dal.getall(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new NewsModel();
                    fur.NewsID = int.Parse(row["NewsID"].ToString());
                    fur.CompanyID = int.Parse(row["CompanyID"].ToString());
                    fur.CompanyName = row["Name"].ToString();
                    fur.ProductID = int.Parse(row["CompanyID"].ToString());
                    fur.ProductName = row["ProductName"].ToString();
                    fur.NewsImg = row["NewsImg"].ToString();
                    fur.Enable = bool.Parse(row["Enable"].ToString());
                    fur.NewsTitle = row["NewsTitle"].ToString();
                    fur.FlagImage = row["FlagImage"].ToString();
                    fur.ViewCount = int.Parse(row["ViewCount"].ToString());
                    fur.NewsHot = bool.Parse(row["NewsHot"].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public int UpdateNews(NewsModel models, int langid)
        {
            if (dal.UpdateNews(models) > 0 && dal.UpdateNewsLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertNews(NewsModel models)
        {
            if (dal.InsertNews(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertNewsLanng(NewsModel models, int langid)
        {
            if (dal.InsertNewsLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteNews(int CarLoanID)
        {
            if (dal.DeleteNews(CarLoanID) > 0 && dal.DeleteNewsLang(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
